//
//  NSString+FIDMatch.m
//  Match
//
//  Created by Fidetro on 2016/10/28.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "NSString+FIDMatch.h"

@implementation NSString (FIDMatch)
//十进制转十六进制字符串
+ (NSString *)decimalToHexadecimal:(NSInteger)num{
    
    return [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%.2lx",(long)num]];
}

+ (NSString *)decimalStringToHexadecimal:(NSString *)decimalString{
    
    if ([decimalString length] % 2 != 0) {
        return @"";
    }
    NSString *hexadecimalCount = [NSString string];
    for (NSInteger length = 0; length<decimalString.length; ) {
        
        NSString *hexadecimal = [NSString decimalToHexadecimal:[[decimalString substringWithRange:NSMakeRange(length, 2)] integerValue]];
        length += 2;
        
        hexadecimalCount = [hexadecimalCount stringByAppendingString:[NSString stringWithFormat:@"%@",hexadecimal]];
    }
    
    return hexadecimalCount;
}


//二进制转十进制
+ (NSString *)binaryToDecimal:(NSString *)decimal
{
    int ll = 0 ;
    int  temp = 0 ;
    for (int i = 0; i < decimal.length; i ++)
    {
        temp = [[decimal substringWithRange:NSMakeRange(i, 1)] intValue];
        temp = temp * powf(2, decimal.length - i - 1);
        ll += temp;
    }
    
    NSString * result = [NSString stringWithFormat:@"%d",ll];
    
    return result;
}

+ (NSInteger)binaryToDecimalSum:(NSString *)binary{
    
    NSInteger location = 0;
    NSInteger sum = 0;
    for (NSInteger i = 0; i<=([binary length] / 8); i++) {
        NSInteger length = [binary length] - location;
        length = length >= 8 ? 8 :length;
        NSInteger num = [[NSString binaryToDecimal:[binary substringWithRange:NSMakeRange(location, length)]]integerValue];
        
        sum += num;
        location += 8;
    }
    
    
    return sum;
    
}

+ (NSString *)binaryToHexadecimal:(NSString *)binary{

    NSInteger location = 0;
    NSString *decimal = @"";
    for (NSInteger i = 0; i<=([binary length] / 8); i++) {
        NSInteger length = [binary length] - location;
        length = length >= 8 ? 8 :length;
        decimal = [NSString stringWithFormat:@"%@%@",decimal,[NSString decimalToHexadecimal:[[NSString binaryToDecimal:[binary substringWithRange:NSMakeRange(location, length)]] integerValue]]];
        
        
        location += 8;
    }
    
    
    return decimal;
    
}

//十六进制转二进制
+ (NSString *)hexadecimalToBinary:(NSString *)hexadecimal
{
    NSMutableDictionary  *hexDic = [[NSMutableDictionary alloc] init];
    
    hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    
    [hexDic setObject:@"0000" forKey:@"0"];
    
    [hexDic setObject:@"0001" forKey:@"1"];
    
    [hexDic setObject:@"0010" forKey:@"2"];
    
    [hexDic setObject:@"0011" forKey:@"3"];
    
    [hexDic setObject:@"0100" forKey:@"4"];
    
    [hexDic setObject:@"0101" forKey:@"5"];
    
    [hexDic setObject:@"0110" forKey:@"6"];
    
    [hexDic setObject:@"0111" forKey:@"7"];
    
    [hexDic setObject:@"1000" forKey:@"8"];
    
    [hexDic setObject:@"1001" forKey:@"9"];
    
    [hexDic setObject:@"1010" forKey:@"A"];
    
    [hexDic setObject:@"1011" forKey:@"B"];
    
    [hexDic setObject:@"1100" forKey:@"C"];
    
    [hexDic setObject:@"1101" forKey:@"D"];
    
    [hexDic setObject:@"1110" forKey:@"E"];
    
    [hexDic setObject:@"1111" forKey:@"F"];
    
    [hexDic setObject:@"1010" forKey:@"a"];
    
    [hexDic setObject:@"1011" forKey:@"b"];
    
    [hexDic setObject:@"1100" forKey:@"c"];
    
    [hexDic setObject:@"1101" forKey:@"d"];
    
    [hexDic setObject:@"1110" forKey:@"e"];
    
    [hexDic setObject:@"1111" forKey:@"f"];
    
    NSMutableString *binaryString=[[NSMutableString alloc] init];
    
    for (int i=0; i<[hexadecimal length]; i++) {
        
        NSRange rage;
        
        rage.length = 1;
        
        rage.location = i;
        
        NSString *key = [hexadecimal substringWithRange:rage];
        
        //NSLog(@"%@",[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]);
        
        binaryString = [[NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]] copy];
        
    }
    
    //NSLog(@"转化后的二进制为:%@",binaryString);
    
    return binaryString;
    
}

//十六进制转十进制
+ (NSString *)hexadecimalToDecimal:(NSString *)hexadecimal hexadecimalLength:(NSInteger)length{
    
    if (hexadecimal == nil) {
        return nil;
    }
    
    NSString *decimal = [NSString stringWithFormat:@"%lu",strtoul([[hexadecimal substringWithRange:NSMakeRange(0, hexadecimal.length)] UTF8String], 0, 16)];
    return decimal;
}

//  十进制转二进制
+ (NSString *)decimalToBinary:(NSString *)binary
{
    NSInteger num = [binary integerValue];
    NSInteger remainder = 0;      //余数
    NSInteger divisor = 0;        //除数
    
    NSString * prepare = @"";
    
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%ld",remainder];
        
        if (divisor == 0)
        {
            break;
        }
    }
    
    NSString * result = @"";
    for (NSInteger i = prepare.length - 1; i >= 0; i --)
    {
        result = [result stringByAppendingFormat:@"%@",
                  [prepare substringWithRange:NSMakeRange(i , 1)]];
    }
    
    return result;
}

+ (NSString *)stringToASCIICode:(NSString *)string{
    NSString *ASCIICode = [NSString string];
    for (NSInteger length = 0; length < string.length; length++) {
        ASCIICode = [ASCIICode stringByAppendingString:[NSString stringWithFormat:@"%d",[string characterAtIndex:length]]];
    }
    
    return ASCIICode;
}

@end
